﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class AddressBO : ChurchDataBusinessBase
    {
        public Address GetRecordByPK(int addressPK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            return (from a in oDB.Addresses
                          where a.AddressPK == addressPK
                          select a).First();
        }

        public Address AddRecord(Address dr)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            oDB.Addresses.InsertOnSubmit(dr);

            dr.LastUpdTimestamp = DateTime.Now;
            dr.LastUpdUser = UserInformation.UserName;
            dr.ActiveInd = true;

            oDB.SubmitChanges();

            return dr;
        }

        public Address UpdateRecord(int addressPK, Address dr)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            Address drA = (from a in oDB.Addresses
                           where a.AddressPK == addressPK
                           select a).First();

            drA.Line1 = dr.Line1;
            drA.Line2 = dr.Line2;
            drA.City = dr.City;
            drA.StateRegion = dr.StateRegion;
            drA.Zipcode = dr.Zipcode;
            drA.Notes = dr.Notes;
            drA.FromDate = dr.FromDate;
            drA.ToDate = dr.ToDate;
            drA.LastUpdUser = UserInformation.UserName;
            drA.LastUpdTimestamp = DateTime.Now;

            oDB.SubmitChanges();

            return drA;
        }

        public void CopyAddressDetails(Address fromAddress, Address toAddress)
        {
            toAddress.Line1 = fromAddress.Line1;
            toAddress.Line2 = fromAddress.Line2;
            toAddress.City = fromAddress.City;
            toAddress.StateRegion = fromAddress.StateRegion;
            toAddress.Zipcode = fromAddress.Zipcode;
            toAddress.Notes = fromAddress.Notes;
            toAddress.FromDate = fromAddress.FromDate;
            toAddress.ToDate = fromAddress.ToDate;
        }
    }
}
